package com.springlearn.springsourcecode.chap20;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class A20 {
    public static final Logger log = LoggerFactory.getLogger(A20.class);

    public static void main(String[] args) throws Exception {
        AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext(WebConfig.class);

        // 解析RequestMapping注解以及派生注解，生成路径与控制器方法的映射关系，在初始化时就生成
        RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
        // 获取映射结果
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        handlerMethods.forEach((k,v)->{
            System.out.println("key:"+ k + "+value"+ v);
        });
        System.out.println("----------------------------------------------------");
        // 请求来了，获取控制器方法，返回处理器执行链对象
        MockHttpServletRequest request1 = new MockHttpServletRequest("POST", "/test2");
        request1.setParameter("name","战胜");
        MockHttpServletRequest request2 = new MockHttpServletRequest("GET", "/test1");

        MockHttpServletRequest request3 = new MockHttpServletRequest("PUT", "/test3");
        request3.addHeader("token","12121212121");


        MockHttpServletRequest request4 = new MockHttpServletRequest("GET", "/test4");


        MockHttpServletResponse response = new MockHttpServletResponse();

        HandlerExecutionChain chain = handlerMapping.getHandler(request4);
        System.out.println(chain);
        System.out.println("----------------------------------------------------");

        MyRequestMappingHandlerAdapter handlerAdapter = context.getBean(MyRequestMappingHandlerAdapter.class);
        ModelAndView modelAndView = handlerAdapter.invokeHandlerMethod(request4, response, (HandlerMethod) chain.getHandler());


        byte[] content = response.getContentAsByteArray();
        System.out.println(new String(content, StandardCharsets.UTF_8));


        System.out.println("》》》》》》》》》》》》》》》》》》》》》》所有参数解析器");
        List<HandlerMethodArgumentResolver> argumentResolvers = handlerAdapter.getArgumentResolvers();
        for (HandlerMethodArgumentResolver argumentResolver : argumentResolvers) {
            System.out.println(argumentResolver);
        }
        System.out.println("》》》》》》》》》》》》》》》》》》》》》所有返回值处理器");
        for (HandlerMethodReturnValueHandler returnValueHandler : handlerAdapter.getReturnValueHandlers()) {
            System.out.println(returnValueHandler);
        }


    }
}
